﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FooFun.DataElements.Dtos;
using FooFun.Faq.Dal.Services;
using PhysicalDataProvider;
using DTOFrameworks;
using System.Collections;

namespace AccessGrant.GrantFrameworks
{
    public class RoleSetQuery
    {
        static RoleSetQuery query;

        public static RoleSetQuery Create()
        {
            if (query == null)
            {
                query = new RoleSetQuery();
            }

            return query;
        }

        RoleSetService roleSetService;

        public IDataElement ListRoleOfUserInForum
            (Guid userId, Guid forumId)
        {
            return null;
        }

        public IDataElement GetRoleOfUserInForum
            (Guid userId, Guid forumId)
        {
            foreach (IDataElement item in setDe)
            {
                if (item["UserId"].Equals(userId))
                {
                    if (item["ForumId"].Equals(Guid.Empty)
                        || item["ForumId"].Equals(forumId))
                    {
                        return item;
                    }
                }
            }

            return new EmptyDataElement("没有设置角色");
        }

        public IDataElement GetRoleOfUser(Guid userId)
        {
            foreach (IDataElement item in setDe)
            {
                if (item["UserId"].Equals(userId))
                {
                    return item;
                }
            }

            return new EmptyDataElement("没有设置角色");
        }

        IDataElement setDe;
        private RoleSetQuery()
        {
            roleSetService = new RoleSetService();
            DEDataProvider dataProvider = new DEDataProvider();

            roleSetService.DataProvider = dataProvider;

            dataProvider.Begin();

            setDe = roleSetService.ListRoleSet();

            dataProvider.End();
        }

        public IDataElement ListForumOfUserInRole(IEnumItem user, IEnumItem role)
        {
            ArrayList arrList = new ArrayList();
            ListDataElement list = new ListDataElement(arrList);

            foreach (IDataElement item in setDe)
            {
                if (item["RoleId"].Equals(role.Id))
                {
                    if (item["UserId"].Equals(user.Id))
                    {
                        list.Add(item);
                    }
                }
            }

            return list;
        }

        public IDataElement ListUserOfRoleInForum(IEnumItem role, IEnumItem forum)
        {
            ArrayList arrList = new ArrayList();
            ListDataElement list = new ListDataElement(arrList);

            foreach (IDataElement item in setDe)
            {
                if (item["RoleId"].Equals(role.Id))
                {
                    if (item["ForumId"].Equals(forum.Id))
                    {
                        list.Add(item);
                    }
                }
            }

            return list;
        }
    }
}
